package com.fdsc.erp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fdsc.erp.entity.FinancialEntity;
import com.fdsc.erp.mapper.ta.FinancialMapper;
import com.fdsc.erp.service.FinancialService;
import com.fdsc.erp.util.Constant;
import com.fdsc.erp.util.PageUtils;
import com.fdsc.erp.util.Query;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

@Service("financialService")
public class FinancialServiceImpl extends ServiceImpl<FinancialMapper, FinancialEntity> implements FinancialService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        String startTime = (params.get("startTime") != null&& !"".equals(params.get("startTime").toString())) ? params.get("startTime") + " 00:00:00" : null;
        String endTime = (params.get("endTime") != null&& !"".equals(params.get("endTime").toString())) ? params.get("endTime") + " 23:59:59" : null;
        String type = params.get("type") != null ? (String) params.get("type") : null;
        Page<FinancialEntity> page = this.selectPage(
                new Query<FinancialEntity>(params).getPage(),
                new EntityWrapper<FinancialEntity>()
                        .eq(StringUtils.isNotBlank(type), "收入或支出", type)
                        .ge(StringUtils.isNotBlank(startTime), "日期", startTime)
                        .le(StringUtils.isNotBlank(endTime), "日期", endTime)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
        );
        return new PageUtils(page);
    }
}
